package cn.edu.nit.sys.service.impl;

import cn.edu.nit.sys.entity.Forum;
import cn.edu.nit.sys.entity.ForumLog;
import cn.edu.nit.sys.mapper.ForumMapper;
import cn.edu.nit.sys.mapper.ReportMapper;
import cn.edu.nit.sys.service.ForumService;
import cn.edu.nit.sys.socket.MessageSocketHandler;
import cn.edu.nit.sys.utils.R;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author 罗文斌
 * @Date 2023/4/11
 * @Version 1.0.0
 */
@Service
@Slf4j
public class ForumServiceImpl extends ServiceImpl<ForumMapper, Forum> implements ForumService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private MessageSocketHandler messageSocketHandler;

    @Override
    public R addForum(Forum forum) {
        forum.setStatus("1");
        return baseMapper.insert(forum) > 0 ? R.ok("发布成功，请等待审核") : R.error("发布失败");
    }

    @Override
    public R getApproveList(Page<Forum> page, String siftName, String siftStatus, String startTime, String endTime, String uid) {
        Page<Forum> list = baseMapper.getApproveList(page, siftName, siftStatus, startTime, endTime, uid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", list.getTotal());
        jsonObject.put("list", list.getRecords());
        return R.ok(jsonObject);
    }

    @Override
    @Transactional
    public R approveForum(ForumLog forumLog) {
        forumLog.setId(UUID.randomUUID().toString().replaceAll("\\-", ""));
        int rs = baseMapper.insertApproveLog(forumLog);
        rs += baseMapper.approveForum(forumLog.getForumId(), forumLog.getResult());
        return rs > 0 ? R.ok("审核成功") : R.error("审核失败");
    }

    @Override
    public R getDetail(String id, String uid) {
        return R.ok(baseMapper.getDetail(id, uid));
    }

    @Override
    public R getForumComment(Page<Map<String, String>> page, String id) {
        Page<Map<String, String>> commentList = baseMapper.getCommentList(page, id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", commentList.getTotal());
        jsonObject.put("list", commentList.getRecords());
        return R.ok(jsonObject);
    }

    @Override
    @Transactional
    public R addForumComment(Map<String, String> params) {
        log.info("参数：{}", params);
        String authorId = baseMapper.getFormAuthor(params.get("forumId"));
        String id = UUID.randomUUID().toString().replaceAll("\\-", "");
        List<String> isBanned = reportMapper.isBanned(params.get("userId"), "0");
        if(isBanned.size() > 0) {
            return R.error("评论失败，当前用户已被禁言");
        }
        int rs = baseMapper.addForumComment(id, params.get("forumId"), params.get("userId"),
                params.get("comment"), params.get("ip"), params.get("lngLat"));
        if(rs > 1) {
            messageSocketHandler.sendMessage(params.get("userId"), authorId, "1", "", params.get("forumId"));
        }
        return rs > 0 ? R.ok("评论成功") : R.error("评论失败");
    }

    @Override
    public R delComment(String id) {
        return baseMapper.delComment(id) > 0 ? R.ok("删除成功") : R.error("删除失败");
    }

    @Override
    public void plusView(String id, String uid) {
        String viewData = baseMapper.getViewData(id, uid);
        if(StringUtils.isEmpty(viewData)) {
            baseMapper.plusView(id);
            String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");
            baseMapper.insertForumLog(uuid, id, uid, 1);
        }
    }

    @Override
    @Transactional
    public R forumLikes(String fid, String uid) {
        String likesRecord = baseMapper.forumLikes(fid, uid);
        String authorId = baseMapper.getFormAuthor(fid);
        if(StringUtils.isEmpty(likesRecord)) {
            int rs = 0;
            rs += baseMapper.plusLikes(fid);
            String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");
            rs += baseMapper.insertForumLog(uuid, fid, uid, 2);
            if(rs > 1) {
                messageSocketHandler.sendMessage(uid, authorId, "1", "", fid);
            }
            return rs > 1 ? R.ok("点赞成功", true) : R.error("点赞失败");
        }else {
            int rs = 0;
            rs += baseMapper.reduceLikes(fid);
            rs += baseMapper.removeLog(fid, uid, 2);
            return rs > 1 ? R.ok("取消点赞", false) : R.error("取消失败");
        }
    }
}
